home *** CD-ROM | disk | FTP | other *** search
/ Freelog 70 / Freelog070.iso / Internet / EasyPHP / easyphp1-8_setup.exe / {app} / phpmyadmin / libraries / dbi / mysqli.dbi.lib.php < prev   
Encoding:
PHP Script  |  2005-01-11  |  11.6 KB  |  340 lines

  1. <?php
  2. /* $Id: mysqli.dbi.lib.php,v 2.30.2.1 2005/01/11 11:53:51 lem9 Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * Interface to the improved MySQL extension (MySQLi)
  7.  */
  8.  
  9. /**
  10.  * Loads the MySQLi extension if it is not loaded yet
  11.  */
  12. if (!@function_exists('mysqli_connect')) {
  13.     PMA_dl('mysqli');
  14. }
  15.  
  16. // check whether mysql is available
  17. if (!@function_exists('mysqli_connect')) {
  18.     require_once('./libraries/header_http.inc.php');
  19.     echo sprintf($strCantLoad, 'mysqli') . '<br />' . "\n"
  20.          . '<a href="./Documentation.html#faqmysql" target="documentation">' . $GLOBALS['strDocu'] . '</a>' . "\n";
  21.     exit;
  22. }
  23.  
  24. // MySQL client API
  25. if (!defined('PMA_MYSQL_CLIENT_API')) {
  26.     $client_api = explode('.', mysqli_get_client_info());
  27.     define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
  28.     unset($client_api);
  29. }
  30.  
  31. // Constants from mysql_com.h of MySQL 4.1.3
  32.  
  33. define('NOT_NULL_FLAG',         1);
  34. define('PRI_KEY_FLAG',          2);
  35. define('UNIQUE_KEY_FLAG',       4);
  36. define('MULTIPLE_KEY_FLAG',     8);
  37. define('BLOB_FLAG',            16);
  38. define('UNSIGNED_FLAG',        32);
  39. define('ZEROFILL_FLAG',        64);
  40. define('BINARY_FLAG',         128);
  41. define('ENUM_FLAG',           256);
  42. define('AUTO_INCREMENT_FLAG', 512);
  43. define('TIMESTAMP_FLAG',     1024);
  44. define('SET_FLAG',           2048);
  45. define('NUM_FLAG',          32768);
  46. define('PART_KEY_FLAG',     16384);
  47. define('UNIQUE_FLAG',       65536);
  48.  
  49. function PMA_DBI_connect($user, $password) {
  50.     global $cfg, $php_errormsg;
  51.  
  52.     $server_port   = (empty($cfg['Server']['port']))
  53.                    ? FALSE
  54.                    : (int) $cfg['Server']['port'];
  55.  
  56.     if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
  57.         $cfg['Server']['socket'] = '';
  58.     }
  59.  
  60.     // NULL enables connection to the default socket
  61.     $server_socket = (empty($cfg['Server']['socket']))
  62.                    ? NULL 
  63.                    : $cfg['Server']['socket'];
  64.  
  65.     $link = mysqli_init();
  66.  
  67.     mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, TRUE);
  68.  
  69.     $client_flags = $cfg['Server']['compress'] && defined('MYSQLI_CLIENT_COMPRESS') ? MYSQLI_CLIENT_COMPRESS : 0;
  70.  
  71.     @mysqli_real_connect($link, $cfg['Server']['host'], $user, $password, FALSE, $server_port, $server_socket, $client_flags);
  72.  
  73.     if (empty($link)) {
  74.         PMA_auth_fails();
  75.     } // end if
  76.  
  77.     PMA_DBI_postConnect($link);
  78.  
  79.     return $link;
  80. }
  81.  
  82. function PMA_DBI_select_db($dbname, $link = NULL) {
  83.     if (empty($link)) {
  84.         if (isset($GLOBALS['userlink'])) {
  85.             $link = $GLOBALS['userlink'];
  86.         } else {
  87.             return FALSE;
  88.         }
  89.     }
  90.     if (PMA_MYSQL_INT_VERSION < 40100) {
  91.         $dbname = PMA_convert_charset($dbname);
  92.     }
  93.     return mysqli_select_db($link, $dbname);
  94. }
  95.  
  96. function PMA_DBI_try_query($query, $link = NULL, $options = 0) {
  97.     if ($options == ($options | PMA_DBI_QUERY_STORE)) {
  98.         $method = MYSQLI_STORE_RESULT;
  99.     } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
  100.         $method = MYSQLI_USE_RESULT;
  101.     } else {
  102.         $method = MYSQLI_USE_RESULT;
  103.     }
  104.  
  105.     if (empty($link)) {
  106.         if (isset($GLOBALS['userlink'])) {
  107.             $link = $GLOBALS['userlink'];
  108.         } else {
  109.             return FALSE;
  110.         }
  111.     }
  112.     if (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION < 40100) {
  113.         $query = PMA_convert_charset($query);
  114.     }
  115.     return mysqli_query($link, $query, $method);
  116. }
  117.  
  118. // The following function is meant for internal use only.
  119. // Do not call it from outside this library!
  120. function PMA_mysqli_fetch_array($result, $type = FALSE) {
  121.     global $cfg, $allow_recoding, $charset, $convcharset;
  122.  
  123.     if ($type != FALSE) {
  124.         $data = @mysqli_fetch_array($result, $type);
  125.     } else {
  126.         $data = @mysqli_fetch_array($result);
  127.     }
  128.  
  129.     /* No data returned => do not touch it */
  130.     if (! $data) return $data;
  131.     
  132.     if (!defined('PMA_MYSQL_INT_VERSION') || PMA_MYSQL_INT_VERSION >= 40100
  133.         || !(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
  134.         /* No recoding -> return data as we got them */
  135.         return $data;
  136.     } else {
  137.         $ret    = array();
  138.         $num    = mysqli_num_fields($result);
  139.         $fields = mysqli_fetch_fields($result);
  140.         $i = 0;
  141.         for ($i = 0; $i < $num; $i++) {
  142.             if (!$meta) {
  143.                 /* No meta information available -> we guess that it should be converted */
  144.                 if (isset($data[$i])) $ret[$i] = PMA_convert_display_charset($data[$i]);
  145.                 if (isset($data[$name])) $ret[PMA_convert_display_charset($name)] = PMA_convert_display_charset($data[$name]);
  146.             } else {
  147.                 /* Meta information available -> check type of field and convert it according to the type */
  148.                 if (stristr($fields[$i]->type, 'BLOB') || stristr($fields[$i]->type, 'BINARY')) {
  149.                     if (isset($data[$i])) $ret[$i] = $data[$i];
  150.                     if (isset($data[$fields[$i]->name])) $ret[PMA_convert_display_charset($fields[$i]->name)] = $data[$fields[$i]->name];
  151.                 } else {
  152.                     if (isset($data[$i])) $ret[$i] = PMA_convert_display_charset($data[$i]);
  153.                     if (isset($data[$fields[$i]->name])) $ret[PMA_convert_display_charset($fields[$i]->name)] = PMA_convert_display_charset($data[$fields[$i]->name]);
  154.                 }
  155.             }
  156.         }
  157.         return $ret;
  158.     }
  159. }
  160.  
  161. function PMA_DBI_fetch_array($result) {
  162.     return PMA_mysqli_fetch_array($result, MYSQLI_BOTH);
  163. }
  164.  
  165. function PMA_DBI_fetch_assoc($result) {
  166.     return PMA_mysqli_fetch_array($result, MYSQLI_ASSOC);
  167. }
  168.  
  169. function PMA_DBI_fetch_row($result) {
  170.     return PMA_mysqli_fetch_array($result, MYSQLI_NUM);
  171. }
  172.  
  173. function PMA_DBI_free_result($result) {
  174.     return @mysqli_free_result($result);
  175. }
  176.  
  177. function PMA_DBI_getError($link = NULL) {
  178.     unset($GLOBALS['errno']);
  179.     if (empty($link)) {
  180.         if (isset($GLOBALS['userlink'])) {
  181.             $link = $GLOBALS['userlink'];
  182.             // Do not stop now. We still can get the error code
  183.             // with mysqli_connect_errno()
  184. //        } else {
  185. //            return FALSE;
  186.         }
  187.     }
  188.  
  189.     if (mysqli_connect_errno()) {
  190.         $error = mysqli_connect_errno();
  191.         $error_message = mysqli_connect_error();
  192.     } elseif ( !empty($link) && mysqli_errno($link)) {
  193.         $error = mysqli_errno($link);
  194.         $error_message = mysqli_error($link);
  195.     } 
  196.  
  197.     // keep the error number for further check after the call to PMA_DBI_getError()
  198.     if (!empty($error)) {
  199.         $GLOBALS['errno'] = $error;
  200.     } else {
  201.         return FALSE;
  202.     }
  203.  
  204.  
  205.     if ($error && $error == 2002) {
  206.         $error = '#' . ((string) $error) . ' - ' . $GLOBALS['strServerNotResponding'];
  207.     } elseif ($error && defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION >= 40100) {
  208.         $error = '#' . ((string) $error) . ' - ' . $error_message;
  209.     } elseif ($error) {
  210.         $error = '#' . ((string) $error) . ' - ' . PMA_convert_display_charset($error_message);
  211.     }
  212.     return $error;
  213. }
  214.  
  215. function PMA_DBI_close($link = NULL) {
  216.     if (empty($link)) {
  217.         if (isset($GLOBALS['userlink'])) {
  218.             $link = $GLOBALS['userlink'];
  219.         } else {
  220.             return FALSE;
  221.         }
  222.     }
  223.     return @mysqli_close($link);
  224. }
  225.  
  226. function PMA_DBI_num_rows($result) {
  227.     return @mysqli_num_rows($result);
  228. }
  229.  
  230. function PMA_DBI_insert_id($link = '') {
  231.     if (empty($link)) {
  232.         if (isset($GLOBALS['userlink'])) {
  233.             $link = $GLOBALS['userlink'];
  234.         } else {
  235.             return FALSE;
  236.         }
  237.     }
  238.     return mysqli_insert_id($link);
  239. }
  240.  
  241. function PMA_DBI_affected_rows($link = NULL) {
  242.     if (empty($link)) {
  243.         if (isset($GLOBALS['userlink'])) {
  244.             $link = $GLOBALS['userlink'];
  245.         } else {
  246.             return FALSE;
  247.         }
  248.     }
  249.     return mysqli_affected_rows($link);
  250. }
  251.  
  252. function PMA_DBI_get_fields_meta($result) {
  253.     // Build an associative array for a type look up
  254.     $typeAr = Array();
  255.     $typeAr[MYSQLI_TYPE_DECIMAL]     = 'real';
  256.     $typeAr[MYSQLI_TYPE_TINY]        = 'int';
  257.     $typeAr[MYSQLI_TYPE_SHORT]       = 'int';
  258.     $typeAr[MYSQLI_TYPE_LONG]        = 'int';
  259.     $typeAr[MYSQLI_TYPE_FLOAT]       = 'real';
  260.     $typeAr[MYSQLI_TYPE_DOUBLE]      = 'real';
  261.     $typeAr[MYSQLI_TYPE_NULL]        = 'null';
  262.     $typeAr[MYSQLI_TYPE_TIMESTAMP]   = 'timestamp';
  263.     $typeAr[MYSQLI_TYPE_LONGLONG]    = 'int';
  264.     $typeAr[MYSQLI_TYPE_INT24]       = 'int';
  265.     $typeAr[MYSQLI_TYPE_DATE]        = 'date';
  266.     $typeAr[MYSQLI_TYPE_TIME]        = 'time';
  267.     $typeAr[MYSQLI_TYPE_DATETIME]    = 'datetime';
  268.     $typeAr[MYSQLI_TYPE_YEAR]        = 'year';
  269.     $typeAr[MYSQLI_TYPE_NEWDATE]     = 'date';
  270.     $typeAr[MYSQLI_TYPE_ENUM]        = 'unknown';
  271.     $typeAr[MYSQLI_TYPE_SET]         = 'unknown';
  272.     $typeAr[MYSQLI_TYPE_TINY_BLOB]   = 'blob';
  273.     $typeAr[MYSQLI_TYPE_MEDIUM_BLOB] = 'blob';
  274.     $typeAr[MYSQLI_TYPE_LONG_BLOB]   = 'blob';
  275.     $typeAr[MYSQLI_TYPE_BLOB]        = 'blob';
  276.     $typeAr[MYSQLI_TYPE_VAR_STRING]  = 'string';
  277.     $typeAr[MYSQLI_TYPE_STRING]      = 'string';
  278.     $typeAr[MYSQLI_TYPE_CHAR]        = 'string';
  279.     $typeAr[MYSQLI_TYPE_GEOMETRY]    = 'unknown';
  280.  
  281.     $fields = mysqli_fetch_fields($result);
  282.     foreach ($fields as $k => $field) {
  283.         $fields[$k]->type = $typeAr[$fields[$k]->type];
  284.         $fields[$k]->flags = PMA_DBI_field_flags($result, $k);
  285.         
  286.         // Enhance the field objects for mysql-extension compatibilty
  287.         $flags = explode(' ', $fields[$k]->flags);
  288.         array_unshift($flags, 'dummy');
  289.         $fields[$k]->multiple_key = (int)(array_search('multiple_key', $flags, true) > 0);
  290.         $fields[$k]->primary_key  = (int)(array_search('primary_key', $flags, true) > 0);
  291.         $fields[$k]->unique_key   = (int)(array_search('unique_key', $flags, true) > 0);
  292.         $fields[$k]->not_null     = (int)(array_search('not_null', $flags, true) > 0);
  293.         $fields[$k]->unsigned     = (int)(array_search('unsigned', $flags, true) > 0);
  294.         $fields[$k]->zerofill     = (int)(array_search('zerofill', $flags, true) > 0);
  295.         $fields[$k]->numeric      = (int)(array_search('num', $flags, true) > 0);
  296.         $fields[$k]->blob         = (int)(array_search('blob', $flags, true) > 0);
  297.     }
  298.     return $fields;
  299. }
  300.  
  301. function PMA_DBI_num_fields($result) {
  302.     return mysqli_num_fields($result);
  303. }
  304.  
  305. function PMA_DBI_field_len($result, $i) {
  306.     $info = mysqli_fetch_field_direct($result, $i);
  307.     // stdClass::$length will be integrated in 
  308.     // mysqli-ext when mysql4.1 has been released.
  309.     return @$info->length;
  310. }
  311.  
  312. function PMA_DBI_field_name($result, $i) {
  313.     $info = mysqli_fetch_field_direct($result, $i);
  314.     return $info->name;
  315. }
  316.  
  317. function PMA_DBI_field_flags($result, $i) {
  318.     $f = mysqli_fetch_field_direct($result, $i);
  319.     $f = $f->flags;
  320.     $flags = '';
  321.     if ($f & UNIQUE_FLAG)         { $flags .= 'unique ';}
  322.     if ($f & NUM_FLAG)            { $flags .= 'num ';}
  323.     if ($f & PART_KEY_FLAG)       { $flags .= 'part_key ';}
  324.     if ($f & SET_FLAG)            { $flags .= 'set ';}
  325.     if ($f & TIMESTAMP_FLAG)      { $flags .= 'timestamp ';}
  326.     if ($f & AUTO_INCREMENT_FLAG) { $flags .= 'auto_increment ';}
  327.     if ($f & ENUM_FLAG)           { $flags .= 'enum ';}
  328.     if ($f & BINARY_FLAG)         { $flags .= 'binary ';}
  329.     if ($f & ZEROFILL_FLAG)       { $flags .= 'zerofill ';}
  330.     if ($f & UNSIGNED_FLAG)       { $flags .= 'unsigned ';}
  331.     if ($f & BLOB_FLAG)           { $flags .= 'blob ';}
  332.     if ($f & MULTIPLE_KEY_FLAG)   { $flags .= 'multiple_key ';}
  333.     if ($f & UNIQUE_KEY_FLAG)     { $flags .= 'unique_key ';}
  334.     if ($f & PRI_KEY_FLAG)        { $flags .= 'primary_key ';}
  335.     if ($f & NOT_NULL_FLAG)       { $flags .= 'not_null ';}
  336.     return PMA_convert_display_charset(trim($flags));
  337. }
  338.  
  339. ?>
  340.